<template>
	<tm-fullView>
		<template v-slot:default="{info}">
			<view :style="{minHeight:info.height+'px'}" :class="[$tm.vx.state().tmVuetify.black?'black':'grey text']">
				<tm-menubars :title="title" color="bg-gradient-primary-lighten" :showback="true"></tm-menubars>
				<tm-form @submit="submit" ref="formData" @request="success" method="post">
					<view class="shadow-24 ">
						<tm-sheet :padding="[16,16]" :shadow="24">
							<view class="mx-32 my-12 border-b-1  pb-12 flex-between"
								:class="$tm.vx.state().tmVuetify.black ? 'bk' : ''">
								<text class="text-size-n ">年度/月度</text>
								<tm-groupradio @change="shiguchechange" :disabled="view">
									<tm-radio v-model="groupchecked.c_1" name="0" label="年度" :disabled="view">
									</tm-radio>
									<tm-radio v-model="groupchecked.c_2" name="1" label="月度" :disabled="view">
									</tm-radio>
								</tm-groupradio>
							</view>

							<view class="mx-32 my-24 border-b-1  pb-24 flex-between"
								:class="$tm.vx.state().tmVuetify.black ? 'bk' : ''">
								<text class="text-size-n ">是否自动续期</text>
								<view>
									<tm-switch v-model="renewal" :disabled="view"></tm-switch>
								</view>
							</view>
							<tm-input name="value" required title="预算总金额" input-type="digit" v-model="reqData.value"
								:disabled="view">
							</tm-input>

							<tm-sheet :padding="[16,16]" :shadow="24">
								<view>
									<tm-input v-for="(item,index) in types" :key="index" :title="item.text"
										:disabled="view" :name="item.id" v-model="inputModal[index]" input-type="digit">
									</tm-input>
								</view>
							</tm-sheet>


							<view class="px-24">
								<tm-button navtie-type="form" theme="bg-gradient-blue-accent" block v-if="!view">提交数据
								</tm-button>
							</view>
						</tm-sheet>
					</view>
				</tm-form>
			</view>
		</template>
	</tm-fullView>
</template>
<script>
	import tmFullView from "@/tm-vuetify/components/tm-fullView/tm-fullView.vue"
	import tmMenubars from "@/tm-vuetify/components/tm-menubars/tm-menubars.vue"
	import tmSheet from "@/tm-vuetify/components/tm-sheet/tm-sheet.vue"
	import tmSwitchList from '@/tm-vuetify/components/tm-switchList/tm-switchList.vue'
	import tmButton from '@/tm-vuetify/components/tm-button/tm-button.vue';
	import tmForm from "@/tm-vuetify/components/tm-form/tm-form.vue"
	import tmPoup from "@/tm-vuetify/components/tm-poup/tm-poup.vue"
	import tmInput from "@/tm-vuetify/components/tm-input/tm-input.vue"
	import tmRadio from "@/tm-vuetify/components/tm-radio/tm-radio.vue"
	import tmGroupradio from "@/tm-vuetify/components/tm-groupradio/tm-groupradio.vue"
	import tmImages from "@/tm-vuetify/components/tm-images/tm-images.vue"
	import tmDivider from "@/tm-vuetify/components/tm-divider/tm-divider.vue"
	import tmSwitch from "@/tm-vuetify/components/tm-switch/tm-switch.vue"
	export default {
		components: {
			tmFullView,
			tmMenubars,
			tmSheet,
			tmSwitchList,
			tmButton,
			tmForm,
			tmPoup,
			tmInput,
			tmRadio,
			tmGroupradio,
			tmImages,
			tmDivider,
			tmSwitch
		},
		data() {
			return {
				er: 1,
				title: '',
				groupchecked: {
					c_1: false,
					c_2: true
				},
				renewal: true,
				reqData: {
					value: '',
					type: 1,
					renewal: 1,
					data: []
				},
				types: [],
				inputModal: [],
				view: false
			}
		},
		onLoad(option) {
			if (option.type == 'add') {
				this.er = option.er
				this.reqData.er = option.er
				if (this.er == 1) {
					this.title = '新增收入预算'
				} else {
					this.title = '新增支出预算'
				}
			} else {
				this.view = true;
				this.title = '查看预算'
				let value = JSON.parse(decodeURIComponent(option.data));
				this.reqData = value
				this.er = value.er
				if(this.reqData.renewal == 1) {
					this.renewal = true
				}else {
					this.renewal = false
				}
				
				if (this.reqData.type == 1) {
					this.groupchecked = {
						c_1: false,
						c_2: true
					}
				} else {
					this.groupchecked = {
						c_1: true,
						c_2: false
					}
				}
			}
			this.loadType()
		},
		onShow() {

		},
		methods: {
			shiguchechange(e) {
				if ((e[0].name == '1' && e[0].checked) || (e[0].name == '0' && !e[0].checked)) {
					this.reqData.type = 1;
				} else {
					this.reqData.type = 0;
				}
			},
			loadType() {
				this.$ajax.get('/type/load/' + this.er).then(res => {
					this.types = res.data
					if (this.view) {
						let types = []
						let arr = []
						for (let i in this.types) {
							let aa = this.types[i]
							let v = ''
							let a = this.types[i];
							for (var j = 0; j < this.reqData.data.length; j++) {
								if (a.id == this.reqData.data[j].type) {
									v = this.reqData.data[j].value
									break;
								}
							}
							
							for (var j = 0; j < this.reqData.jihua.length; j++) {
								if (a.id == this.reqData.jihua[j].type) {
									aa.text  = aa.text + '('+this.reqData.jihua[j].value+')'
									break;
								}
							}
							arr.push(v)
							types.push(aa)
						}
						this.types = types
						this.inputModal = arr
					}
				})
			},
			submit(e) {
				if (e === false) {
					uni.$tm.toast("请填写必填项。")
				} else {
					if (!this.isNumber(this.reqData.value)) {
						uni.$tm.toast("数据不合理。")
						return;
					}
					let data = this.inputModal;
					let arr = []
					let money = 0;
					for (let i in data) {
						let v = data[i];
						if (null != v) {
							if (!this.isNumber(v)) {
								uni.$tm.toast("数据不合理。")
								return;
							}
							let tid = this.types[i].id

							let data = {
								type: tid,
								value: v
							}
							arr.push(data)
							money = money + parseFloat(v)
						}
					}
					if (parseFloat(money) > parseFloat(this.reqData.value)) {
						uni.$tm.toast("分配金额大于总金额。")
						return;
					}
					this.reqData.data = arr
					if (this.renewal) {
						this.reqData.renewal = 1
					} else {
						this.reqData.renewal = 0
					}
					this.judgeLogin(res => {
						this.reqData.uid = res.id
						this.$ajax.post("/budget/save", this.reqData).then(res => {
							if (res.code == 1) {
								uni.$tm.toast("保存成功")
								uni.navigateBack()
							}
						})
					})
				}

			},
			isNumber(val) {
				var regPos = /^[0-9]+$/; //非负浮点数
				var regNeg = /^[0-9]+\.?[0-9]+?$/; //负浮点数
				if (regPos.test(val) || regNeg.test(val)) {
					return true;
				} else {
					return false;
				}

			},
			async getData(e) {
				let d = await this.$refs.formData.getFormData();
				uni.showModal({
					title: "数据如下",
					content: JSON.stringify(d),
					showCancel: false
				})
			},
			success(e) {
				//提交成功 。
				uni.$tm.toast(e.msg)
			},
		}
	}
</script>

<style>

</style>
